#ifndef CRYPTO_UTILS_H
#define CRYPTO_UTILS_H

#include "common.h"

// SM2密钥生成
int generate_sm2_keypair(const char *private_key_file, const char *public_key_file);

// SM4加密
int sm4_encrypt(const unsigned char *plaintext, size_t plaintext_len,
                const unsigned char *key, unsigned char **ciphertext, size_t *ciphertext_len);

// SM4解密
int sm4_decrypt(const unsigned char *ciphertext, size_t ciphertext_len,
                const unsigned char *key, unsigned char **plaintext, size_t *plaintext_len);

// SM2加密
int sm2_encrypt(EVP_PKEY *pub_key, const unsigned char *plaintext, size_t plaintext_len,
                unsigned char **ciphertext, size_t *ciphertext_len);

// SM2解密
int sm2_decrypt(EVP_PKEY *priv_key, const unsigned char *ciphertext, size_t ciphertext_len,
                unsigned char **plaintext, size_t *plaintext_len);

// SM2签名
int sm2_sign(EVP_PKEY *priv_key, const unsigned char *message, size_t message_len,
             unsigned char **signature, size_t *signature_len);

// SM2验证
int sm2_verify(EVP_PKEY *pub_key, const unsigned char *message, size_t message_len,
               const unsigned char *signature, size_t signature_len);

// 加载公钥/私钥
EVP_PKEY *load_public_key(const char *filename);
EVP_PKEY *load_private_key(const char *filename);

// 数字信封操作
int create_digital_envelope(const unsigned char *plaintext, size_t plaintext_len,
                           EVP_PKEY *sender_priv, EVP_PKEY *receiver_pub,
                           DigitalEnvelope *envelope);

int extract_digital_envelope(DigitalEnvelope *envelope,
                            EVP_PKEY *receiver_priv, EVP_PKEY *sender_pub,
                            unsigned char **plaintext, size_t *plaintext_len);

void free_digital_envelope(DigitalEnvelope *envelope);

#endif
